use serde::{Deserialize, Serialize};

use crate::database::{self, table::link::model::NewLink};

#[derive(Debug, Serialize, Deserialize)]
pub struct LinkForm {
    pub id: i32,
    pub platform: String,
    pub url: String,
    pub tid: i32,
}

#[derive(Debug, Serialize, Deserialize)]
pub struct NewLinkForm {
    pub platform: String,
    pub url: String,
    pub tid: i32,
}

impl From<&database::table::link::model::Link> for LinkForm {
    fn from(link: &database::table::link::model::Link) -> Self {
        LinkForm {
            id: link.id,
            platform: link.platform.to_string(),
            url: link.url.to_string(),
            tid: link.tid,
        }
    }
}

impl LinkForm {
    pub fn to(&self) -> NewLinkForm {
        NewLinkForm {
            platform: self.platform.clone(),
            url: self.url.clone(),
            tid: self.tid,
        }
    }
}

impl NewLinkForm {
    pub fn to(&self) -> NewLink {
        NewLink {
            platform: &self.platform,
            url: &self.url,
            tid: self.tid,
        }
    }
}
